﻿
//nickname的DOM对象
var nickNameDom = document.getElementById("nickname");
var nickNameMsg = document.getElementById("nicknameMsg");
var passwordMsg = document.getElementById("passwordMsg");
var confrepasswordMsg = document.getElementById("confrepasswordMsg");
var passwordDom = document.getElementById("password");
var confrepasswordDom = document.getElementById("confrepassword");
var phonedMsg = document.getElementById("phonedMsg");
var userPhoneDom = document.getElementById("UserPhone");

nickNameDom.value = "香草波波";
nickNameMsg.style = "display:none";

function nicknameCheck() {
    var nickName = nickNameDom.value;
    var leng = nickName.length;
    if (!nickName) {
        nickNameMsg.style = "display:block";
        nickNameMsg.innerHTML = "用户名必须填写";
        return false;
    }
    else if (leng < 5) {
        nickNameMsg.style = "display:block";
        nickNameMsg.innerHTML = "用户名必须是超过4个字";
        return false;
    }
    else {
        // document.getElementById("nicknameMsg").innerHTML = "<a href='#' class='text-green '>√</h3>";
        nickNameMsg.style = "display:none";
        return true;
    }
}

function checkPassword(obj) {
    console.log(obj.id);
    if (obj.id == "password") {
        //密码
        if (!checkPasswordIsNull(obj, passwordMsg)) {
            return false;
        }
        if (!checkPasswordEqu(obj.value, confrepasswordDom.value)) {
            passwordMsg.innerHTML = "两次密码不一致";
            return false;
        } else {
            passwordMsg.innerHTML = "";
            return true;
        }
    }
    else {
        //确认密码
        if (!checkPasswordIsNull(obj, confrepasswordMsg)) {
            return false;
        }
        if (!checkPasswordEqu(obj.value, confrepasswordDom.value)) {
            confrepasswordMsg.innerHTML = "两次密码不一致";
            return false;
        } else {
            confrepasswordMsg.innerHTML = "";
            return true;
        }
    }
}

function checkPasswordIsNull(obj, objMsg) {
    if (!obj) {
        return false;
    }
    else if (!obj.value || obj.value.length < 1) {
        objMsg.innerHTML = "输入密码";
        return false;
    }
    else if (!obj.value || obj.value.length < 5) {
        objMsg.innerHTML = "密码的长度必须大于4";
        return false;
    }
    return true;
}

/// v=密码  cv=确认密码
function checkPasswordEqu(v, cv) {
    if (v === cv) {
        return true;
    } else {
        return false;
    }
}

function validatePhone(obj) {
    let phoneval = obj.value; //
    if (!(/^1(3|5|6|7|8|9)\d{9}$/.test(phoneval))) {
        phonedMsg.innerHTML = "手机号格式不正确";
        return false;
    }
    return true;
}

/*
 * 此处提交表单
 * */
function regsubmit() {
    //regForm
    //校验用户名称
    if (!nicknameCheck()) {
        alert("用户名校验不通过，提交不成功！")
        return false;
    }
    //校验密码
    if (!checkPassword(passwordDom)) {
        alert("密码校验不通过，提交不成功！")
        return false;
    }
    if (!checkPassword(confrepasswordDom)) {
        alert("确认密码校验不通过，提交不成功！")
        return false;
    }


    //var formDomObj = document.getElementById("regForm");
    ////formDomObj.method = "post";
    ////formDomObj.acction="/Register/SubmitReg"
    //formDomObj.submit();

    var objRequest = {
        UserName: nickNameDom.value,
        UserPassword: passwordDom.value,
        UserPhone: userPhoneDom.value
    }

   
    $.ajax({
        url: '/Register/SubmitReg',
        method: 'post',
        ansync: false,
        data: objRequest,
        success(data) {
            if (data.code == 200) {
                window.location.href = "/Login/Index";
            } else {
                alert(data.msg);
                return;
            }
        }
    });


}
